<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert ein Spielort
 * Methoden, um Spielorte aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Gamevenue {
    private $id = null;
    private $name = null;
    private $city = null;
    private $maplink = null;


	public function __construct($data = array()) {
        if (isset($data['gamevenue_id'])) $this->id = (int) $data['gamevenue_id'];
        if (isset($data['gamevenue_name'])) $this->name = $data['gamevenue_name'];
        if (isset($data['gamevenue_city'])) $this->city = $data['gamevenue_city'];
        if (isset($data['gamevenue_maplink'])) $this->maplink = $data['gamevenue_maplink'];
    }
	
	public static function getGamevenueById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_gamevenues WHERE gamevenue_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Gamevenue($row);
    }
	
	public static function getGamevenueList() {
        $db = new Database();

        $sql = "SELECT * FROM ff_gamevenues";

        $stmt = $db->prepare( $sql );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $gamevenue = new Gamevenue($row);
            $list[] = $gamevenue;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;

        return (array("gamevenueList" => $list, "totalRows" => $totalRows[0]));
    }
	
	public function insert() {
        if ($this->id != 0) {
            trigger_error("Gamevenue::insert(): Es wird versucht ein neuer Spielort anzulegen, obwohl bereits die ID gesetzt wurde (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_gamevenues (gamevenue_name, gamevenue_city, gamevenue_maplink) VALUES (:name, :city, :maplink)";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":city", $this->city, PDO::PARAM_STR);
        $stmt->bindValue(":maplink", $this->maplink, PDO::PARAM_STR);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Gamevenue::update(): Es wird versucht ein Spielort zu speichern, der keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_gamevenues SET gamevenue_name=:name, gamevenue_city=:city, gamevenue_maplink=:maplink WHERE gamevenue_id=:id";
		
        $stmt = $db->prepare($sql);
		$stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":city", $this->city, PDO::PARAM_STR);
        $stmt->bindValue(":maplink", $this->maplink, PDO::PARAM_STR);
		$stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Gamevenue::delete(): Es wird versucht ein Spielort zu loeschen, der keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_gamevenues WHERE gamevenue_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getCity() {
        return $this->city;
    }

    public function getMaplink() {
        return $this->maplink;
    }


}
?>
